package site.qjdb.service.impl;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import site.qjdb.dao.ChatDao;
import site.qjdb.dao.OrderDao;
import site.qjdb.pojo.Chat;
import site.qjdb.pojo.ChatItem;
import site.qjdb.pojo.OrderItem;
import site.qjdb.pojo.User;
import site.qjdb.service.ChatService;
import site.qjdb.util.CommonUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ChatServiceImpl implements ChatService {
	@Resource
	private ChatDao chatDao;
	@Resource
	private OrderDao orderDao;
	
	@Override
	public void addChatByOrder(List<Chat> chatList, List<ChatItem> chatItemList) {
		// 查看是否已有聊天
		for(int i = 0; i < chatList.size(); i++) {
			Chat chat = chatList.get(i);
			// 设置me和notMe的其他聊天不是打开的
			if(i == chatList.size() - 1) {
				Chat _chat = new Chat();
				_chat.setMe(chat.getMe());
				_chat.setIsOpen(0);
				chatDao.updateChat(_chat);
				_chat.setMe(chat.getNotMe());
				chatDao.updateChat(_chat);
			}
			List<Chat> newChatList = chatDao.selectChatAll(chat);
			chat.setIsOpen(1);
			chat.setIsRecent(1);
			chat.setRecentContent("我预约了你的商品");
			ChatItem chatItem = chatItemList.get(i);
			if(newChatList == null || newChatList.size() == 0) {
				// 设置已读行数
				chat.setReadCount(0);
				// 添加聊天，添加两个me和notMe互换，但uuid相同
				chat.setUuid(CommonUtils.uuid());
				chatDao.addChat(chat);
				// 互换me、notMe
				User temp = chat.getMe();
				chat.setMe(chat.getNotMe());
				chat.setNotMe(temp);
				chatDao.addChat(chat);
				// 给聊天子项设置相同的uuid
				chatItem.setUuid(chat.getUuid());
				// 添加聊天子项
				chatDao.addChatItem(chatItem);
			} else {
				Chat newChat = newChatList.get(0);
				// 更改聊天信息
				chatDao.updateChat(chat);
				// 互换me、notMe，使isOpen和recentContent在对方也实现
				User temp = chat.getMe();
				chat.setMe(chat.getNotMe());
				chat.setNotMe(temp);
				chatDao.updateChat(chat);
				chatItem.setUuid(newChat.getUuid());
				// 添加聊天子项
				chatDao.addChatItem(chatItem);
			}
		}
	}
	
	@Override
	public int findNewCount(Integer me) {
		int newCount = 0;
		// 不关联查询
		List<Chat> chatList = chatDao.selectChatAll0(me);
		for(Chat chat : chatList) {
			ChatItem chatItem = new ChatItem();
			chatItem.setUuid(chat.getUuid());
			List<ChatItem> chatItemList = chatDao.selectChatItem(chatItem);
			int total = chatItemList.size();
			newCount = newCount + (total - chat.getReadCount());
		}
		return newCount;
	}
	
	@Override
	public List<Chat> findRecent(Integer me) {
		Chat _chat = new Chat();
		_chat.setIsRecent(1);
		User user = new User();
		user.setUid(me);
		_chat.setMe(user);
		// 查找聊天
		List<Chat> chatList = chatDao.selectChatAll(_chat);
		for(Chat chat : chatList) {
			String uuid = chat.getUuid();
			ChatItem _chatItem = new ChatItem();
			_chatItem.setUuid(uuid);
			// 查找该聊天的所有子项
			List<ChatItem> chatItemList = chatDao.selectChatItem(_chatItem);
			for(ChatItem chatItem : chatItemList) {
				if(chatItem.getIid() != null) {
					// 说明是订单信息
					OrderItem orderItem = new OrderItem();
					orderItem.setIid(chatItem.getIid());
					List<OrderItem> orderItemList = orderDao.selectOrderItemAll(orderItem);
					if(orderItemList == null || orderItemList.size() == 0) {
						throw new RuntimeException("根据聊天子项的iid找不到对应的订单子项");
					}
					// 设置订单子项
					chatItem.setOrderItem(orderItemList.get(0));
				}
			}
			// 添加聊天子项
			chat.setChatItemList(chatItemList);
		}
		return chatList;
	}

	@Override
	public void clearNew(Integer me, Integer notMe) {
		// 查询聊天子项总数量
		ChatItem chatItem = new ChatItem();
		chatItem.setSendId(me);
		chatItem.setReceivedId(notMe);
		int total = chatDao.selectChatItemCount(chatItem);
		// 若找到了则设置已读行数
		if(total > 0) {
			Chat chat = new Chat();
			User meUser = new User();
			meUser.setUid(me);
			User notMeUser = new User();
			notMeUser.setUid(notMe);
			chat.setMe(meUser);
			chat.setNotMe(notMeUser);
			chat.setReadCount(total);
			chatDao.updateChat(chat);
		}
	}

	@Override
	public void clearRecent(Integer me, Integer notMe) {
		Chat chat = new Chat();
		User meUser = new User();
		meUser.setUid(me);
		User notMeUser = new User();
		notMeUser.setUid(notMe);
		chat.setMe(meUser);
		chat.setNotMe(notMeUser);
		chat.setIsRecent(0);
		chat.setIsOpen(0);
		chatDao.updateChat(chat);
	}

	@Override
	public void setOpen(Integer me, Integer notMe) {
		// 清除现有的已打开
		Chat _chat = new Chat();
		User meUser = new User();
		meUser.setUid(me);
		_chat.setMe(meUser);
		_chat.setIsOpen(0);
		chatDao.updateChat(_chat);
		// 设置当前为已打开
		User notMeUser = new User();
		notMeUser.setUid(notMe);
		_chat.setNotMe(notMeUser);
		_chat.setIsOpen(1);
		chatDao.updateChat(_chat);
	}

}
